#include "table.h"

#include "tablecolumnlist.h"
#include "tableconstraintlist.h"
#include "tablecolumn.h"
#include "tableconstraint.h"
#include "truthValue.h"
#include "symbolicVariable.h"
#include "schemacomponentformula.h"
#include "formula.h"
#include "columnlist.h"

#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;

Table::Table(TableColumnList * columns, TableConstraintList * constraints,const string & name, int rows):SchemaComponent(name)
{
    if(columns != NULL) this->columns = columns->clone();
    else this->columns = NULL;

    if(constraints != NULL) this->constraints = constraints->clone();
    else this->constraints = NULL;

    this->rows = rows;
}

Table::~Table()
{
    delete columns; columns = NULL;
    delete constraints;  constraints = NULL;
    rows = 0;
}

Table::Table(const Table& other):SchemaComponent(other.name)
{
    if(other.columns != NULL) columns = other.columns->clone();
    else columns =NULL;

    if(other.constraints != NULL) constraints = other.constraints->clone();
    else this->constraints = NULL;

    rows = other.rows;
}

void Table::addConstraint(TableConstraint * constraint)
{
    if(constraints == NULL) constraints = new TableConstraintList();
    if(constraint != NULL) constraints->addTableConstraint(constraint);
}

void Table::addColumn(TableColumn * column)
{
    if(columns == NULL) columns = new TableColumnList();
    if(column != NULL) columns->addColumn(column);
}

void Table::addColumn(const string & name, TableColumnType type)
{
    if(columns == NULL) columns = new TableColumnList();
    columns->addColumn(name,type);
}

TableConstraintList* Table::getConstraints() const
{
    return constraints;
}

TableColumnList* Table::getTableColumns() const
{
    return columns;
}

void Table::setConstraints(TableConstraintList * constraints)
{
    delete this->constraints; this->constraints = NULL;
    this->constraints = constraints->clone();
}

void Table::setColumns(TableColumnList * columns)
{
    delete this->columns; this->columns = NULL;
    this->columns = columns->clone();
}

string Table::toString()
{
    string str = "TABLE: " + name;

    str = str + "\nCOLUMNS:\n";
    if(columns != NULL){
         str = str + columns->toString();
    }//if

    str = str + "\nTABLE CONSTRAINTS:\n";
    if(constraints != NULL){
        str = str + constraints->toString() + "\n";
    }//if

    char buffer [33];
	sprintf(buffer,"%d",rows);
	string str_rows(buffer);
	str = str + "ROWS = " + str_rows;
    return str;
}

int Table::getRows()
{
    return rows;
}

void Table::setRows(int rows)
{
    this->rows = rows;
}

ColumnList * Table::getColumns(){
	std::vector<TableColumn*> * cols = columns->getColumns();
	ColumnList * cl= new ColumnList();
	for (int i =0; i<cols->size();i++){
		cl->addColumn(cols->at(i)->getName());
	}

	return cl;

}


SchemaComponent * Table::clone()
{
    return new Table(*this);
}

TableColumnType Table::getColumnType(const string & name)
{
    if(columns != NULL) return columns->getColumnType(name);
    else {
		TableColumnType t ;
		t.type = UNKNOWN_TYPE;
		return t;
	}
}

